﻿//-----------------------------------------------------------------------
// <copyright file="ViewDefinitionManager.cs" company="n.a.">
//     Copyright Dave999. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "To be returned to .")]

/*
Todo: Get view selection combo working correctly
		- re-order: do this on save with the view def manager
      descending sort implemented
      Extra filter.
      Filter clause
      search field types eg. numeric search not working
*/

namespace DbView
{
	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Xml;
	using System.Data;

	/*
		This class manages the interplay between the detache node-based ViefDefinition
		class and the Document that holds the view definition list.
		It needs to manage 
	*/
	public class ViewDefinitionManager
	{
		// Node makers
		public static ViewDefinition makeNewViewDefinition()
		{
			XmlNode node = DbView.Resources.ConfigDoc.Instance.CreateElement(DbView.Resources.ConfigDoc.CFGELM_VIEWDEF);
			return new ViewDefinition(DbView.Resources.ConfigDoc.Instance, node);
		}
		public static ViewDefinition makeNewViewDefinition(DataTable dt)
		{
			ViewDefinition viewDef = makeNewViewDefinition();
			viewDef.ConfigureFromTable(dt);
			return viewDef;
		}
		public static ViewDefinition makeNewViewDefinition(XmlNode node)
		{
			return new ViewDefinition(DbView.Resources.ConfigDoc.Instance, node);
		}

		// either add or replace
		public static void AddOrReplace(ViewDefinition viewDef, string connectionTag)
		{
			// find the node //Views/View[Name='']
			String xpath = string.Format("//{0}/{1}[@Name='{2}']/OWNERS/OWNER[@Name='{3}']/../..",
										 DbView.Resources.ConfigDoc.CFGELM_VIEWDEFS,
										 DbView.Resources.ConfigDoc.CFGELM_VIEWDEF,
										 viewDef.Name,
										 connectionTag);
			// attach the connection tag to the view.
			viewDef.SetOwner(connectionTag, true);
			// perform the actual add or replace.
			XmlNode oldNode = DbView.Resources.ConfigDoc.Instance.SelectSingleNode(xpath);
			if (oldNode == viewDef.Node)
				return; // already attached
			if (oldNode != null)
			{
				// add new just above old
				DbView.Resources.ConfigDoc.Instance.Views.InsertBefore(viewDef.Node, oldNode);
				// whip away the old leaving the new in place of the old.
				DbView.Resources.ConfigDoc.Instance.RemoveRecord(oldNode);
			}
			else
			{
				DbView.Resources.ConfigDoc.Instance.Views.AppendChild(viewDef.Node);
			}
		}

		public static void Remove(ViewDefinition viewDef)
		{
			DbView.Resources.ConfigDoc.Instance.RemoveChild(viewDef.Node);
		}

		public static ViewDefinition FindView(String nameKey, string connectionTag)
		{
			// find the node //Views/View[Name='']
			String xpath = string.Format("//{0}/{1}[@Name='{2}']/OWNERS/OWNER[@Name='{3}']/../..",
										 DbView.Resources.ConfigDoc.CFGELM_VIEWDEFS,
										 DbView.Resources.ConfigDoc.CFGELM_VIEWDEF,
										 nameKey,
										 connectionTag
										 );
			XmlNode tgtNode = DbView.Resources.ConfigDoc.Instance.SelectSingleNode(xpath);
			if (tgtNode == null) return null; // It's not there!!
			return makeNewViewDefinition(tgtNode);
		}

		public static List<String> FindViewListForConnection(String connectionTag)
		{
			List<String> viewNames = new List<string>();
			String xpath = string.Format("//{0}/{1}/OWNERS/OWNER[@Name='{2}']/../..",
										 DbView.Resources.ConfigDoc.CFGELM_VIEWDEFS,
										 DbView.Resources.ConfigDoc.CFGELM_VIEWDEF,
										 connectionTag);
			XmlNodeList tgtNodes = DbView.Resources.ConfigDoc.Instance.SelectNodes(xpath);
			foreach (XmlNode node in tgtNodes)
			{
				viewNames.Add(node.Attributes["Name"].Value);
			}
			return viewNames;
		}

		// this version only returns saved deals. The version above returns too much for the view
		// list and led to bad behaviour.
		public static List<String> FindSavedViewListForConnection(String connectionTag)
		{
			List<String> viewNames = new List<string>();
			String xpath = string.Format("//{0}/{1}[not(@Temp)]/OWNERS/OWNER[@Name='{2}']/../..",
										 DbView.Resources.ConfigDoc.CFGELM_VIEWDEFS,
										 DbView.Resources.ConfigDoc.CFGELM_VIEWDEF,
										 connectionTag);
			XmlNodeList tgtNodes = DbView.Resources.ConfigDoc.Instance.SelectNodes(xpath);
			foreach (XmlNode node in tgtNodes)
			{
				viewNames.Add(node.Attributes["Name"].Value);
			}
			return viewNames;
		}

	}
}
